package com.example.hanchen.yongkanggy.http;

import android.util.Log;

import com.example.hanchen.yongkanggy.entity.ImageEntity;
import com.google.gson.Gson;

import java.io.File;
import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PostFile {

    public void postFiles(String url, File file) {
        OkHttpClient client = new OkHttpClient();

        // MediaType.parse() 需要一个合法的媒体类型
        MediaType mediaType = MediaType.parse("multipart/form-data");

        // 创建RequestBody，使用MultipartBody.Builder创建
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(mediaType, file))
                .addFormDataPart("moduleName", "Question Feedback")
                .build();

        // 创建Request
        Request request = new Request.Builder()
                .url(url) // 目标地址
                .post(requestBody)
                .build();

        // 发送请求
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                e.printStackTrace();
                Log.i("JFOEIFJEIFDDDDD", "成功");
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
//                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                Log.i("JFOEIFJEIFDDDDD", "失败");
                // 处理响应
                System.out.println(response.body().string());
            }
        });
    }


    public void postFileAndModuleName(String url, File file, String moduleName) {
        OkHttpClient client = new OkHttpClient();

        // 创建MultipartBody.Builder，并设置类型为表单数据
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        // 添加文件
        builder.addFormDataPart("file", file.getName(), RequestBody.create(file, MediaType.parse("multipart/form-data")));
        // 添加moduleName
        builder.addFormDataPart("moduleName", moduleName);
        // 创建RequestBody
        RequestBody requestBody = builder.build();
        // 创建Request
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();


        // 发送请求
        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                e.printStackTrace();
                Log.i("JFOEIFJEIFDDDDD", "成功");
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
//                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
                Log.i("JFOEIFJEIFDDDDD", "失败");
                // 处理响应
                System.out.println(response.body().string());
            }
        });


//        // 发送请求
//        try (Response response = client.newCall(request).execute()) {
//            if (response.isSuccessful()) {
//                // 请求成功
//                Log.i("JFOEIFJEIFDDDDD", "成功");
//            } else {
//                // 请求失败
//                Log.i("JFOEIFJEIFDDDDD", "失败");
//            }
//        } catch (IOException e) {
//            // 处理异常
//        }
    }


    public String sendPostWithOkHttp(String url, File tempFile) {
        Log.i("JFOEIJIOE", "url " + url + "  file  " + tempFile);

        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), tempFile);
        MultipartBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", tempFile.getName(), requestBody)
                .addFormDataPart("moduleName", "footerImg")
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                e.printStackTrace();
                Log.i("JFOEIFJEIFDDDDD", "失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
//                if (response.isSuccessful()) {
//                Log.i("JFOEIFJEIFDDDDD", "成功" + "  " + response.body().string());
                String responseStr = response.body().string();
                filemanager(responseStr);
                // 处理响应
//                }
            }
        });
        return url;
    }

    private void filemanager(String data) {
        Gson gson = new Gson();
        ImageEntity imageEntity = gson.fromJson(data, ImageEntity.class);
        if (imageEntity.getCode() == 200) {

        }
        /*清除缓存*/
//        webView.clearCache(true);
    }
}
